from django.db import models
from django.utils.text import slugify

from datetime import datetime
# Create your models here.
import sys
sys.path.insert(0, '../')
from apps.users.models import MyUser

from common.base_model import BaseModel
from ckeditor_uploader.fields import RichTextUploadingField

class VideosCategory(models.Model):
    id = models.AutoField(primary_key=True)
    name=models.CharField(max_length=50,verbose_name='分类名称',default='')
    parent=models.ForeignKey("self", null=True,blank=True,verbose_name="父类",on_delete=models.DO_NOTHING,related_name="sub_cat")
    is_nav=models.BooleanField(default=False,verbose_name='是否显示在导航栏')


    def __str__(self):
        return self.name

    class Meta:
        verbose_name='视频分类'
        verbose_name_plural=verbose_name

def video_upload_path1(instance, filename):
    '''
     该函数重新返回上传路径
     示例：
     videos/username/filename.ext
     其中username为MyUser表中username字段的值
     并使用slugify来保证文件名是URL友好的
    '''
    # 假设MyUser有一个name字段
    user_name = instance.user.username if instance.user else '匿名'
    # 使用slugify来确保文件名是URL友好的
    user_name_slug = slugify(user_name)
    # 构造上传路径，例如："videos/用户名/文件名.ext"
    return f'videos/{user_name_slug}/{filename}'
def video_upload_path(instance, filename):
    '''
     该函数重新返回上传路径
     示例：
     videos/username/filename.ext
     其中username为MyUser表中username字段的值
     并使用slugify来保证文件名是URL友好的
    '''
    # 假设MyUser有一个name字段
    user_name = str(instance.id)
    # 使用slugify来确保文件名是URL友好的
    user_name_slug = slugify(user_name)
    # 构造上传路径，例如："videos/用户名/文件名.ext"
    return f'videos/{user_name_slug}/original.mp4'
class Video_web1(models.Model):
    file =models.FileField(default=None,upload_to=video_upload_path,verbose_name="视频文件")
    m3u8 = models.FileField(default=None,blank=True, null=True,verbose_name="m3u8文件")
    picture = models.ImageField(default=None,verbose_name="封面图片文件")
    name = models.CharField(default=None,max_length=128,verbose_name="视频名称")
    video_desc=RichTextUploadingField(default='',blank=True, null=True, verbose_name='视频详情')
    click_num = models.IntegerField(default=0, blank=True, null=True,verbose_name="点击数")
    fav_num = models.IntegerField(default=0, blank=True, null=True,verbose_name="收藏数")
    user = models.ForeignKey(MyUser,default=None,blank=True, null=True, verbose_name="用户", on_delete=models.DO_NOTHING)
    ishidden = models.BooleanField(default=False,blank=True, null=True,verbose_name="是否隐藏")
    is_recommend = models.BooleanField(default=False, blank=True, null=True,verbose_name="是否推荐")
    category = models.ForeignKey(VideosCategory, blank=True, null=True, verbose_name='商品分类',
                                 on_delete=models.DO_NOTHING)
    createdate = models.DateTimeField(default=datetime.now,blank=True, null=True, verbose_name='创建时间')

    def __str__(self):
        return self.name

    class Meta:
            verbose_name = '视频信息' # 模型名称(单数)
            verbose_name_plural = verbose_name # 模型名称(复数)

